<?php


namespace lib\tools\curl;


/**
 * CURL，全称 Command Line URL Viewer
 * URL是 Uniform Resource Location的缩写
 * Class multipleCurl
 * @package lib\tools
 */
class multipleCurl
{

    public static function single()
    {
        $testUrl = 'http://liyutong180220.top/English/search';
        $milliseconds_timeout = 1000;
        $ch = curl_init($testUrl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //The maximum number of milliseconds to allow cURL functions to execute.
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $milliseconds_timeout);
        //The number of milliseconds to wait while trying to connect. Use 0 to wait indefinitely.
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $milliseconds_timeout);
        //Perform a cURL session
        //Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
        $result = curl_exec($ch);
        //Returns the error number for the last cURL operation.
        $errno = curl_errno($ch);
        //Return a string containing the last error for the current session
        $error = curl_error($ch);

        //场景:实现cookie共享,提高客户端与服务器之间的传输性能
//        $sh = curl_share_init();

        //Get information regarding a specific transfer
        //If opt is given, returns its value. Otherwise, returns an associative array with the following elements (which correspond to opt), or FALSE on failure
//        $all = curl_getinfo($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $totalTime = curl_getinfo($ch, CURLINFO_TOTAL_TIME);

        //Closes a cURL session and frees all resources. The cURL handle, ch, is also deleted.
        curl_close($ch);

        return compact('httpCode', 'errno', 'error', 'result', 'totalTime');
    }

    public static function test()
    {
        $testUrl = 'http://liyutong180220.top/English/search';
        $lines = 100;
        $milliseconds_timeout = 10000;
        $chStack = [];
        // create cURL resources
        for ($i = 0; $i < $lines; $i++) {
            $chStack[$i] = curl_init();
        }

        //create the multiple cURL handle
        $mh = curl_multi_init();

        // set URL and other appropriate options
        foreach ($chStack as $ch) {
            curl_setopt($ch, CURLOPT_URL, $testUrl);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_TIMEOUT_MS, $milliseconds_timeout);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS,$milliseconds_timeout);
            //add handles
            curl_multi_add_handle($mh, $ch);
        }

        //execute the multi handle
        do {
            $status = curl_multi_exec($mh, $active);
            if ($active) {
                curl_multi_select($mh);
            }

            while (false !== curl_multi_info_read($mh));

        } while ($active && $status == CURLM_OK);

        $error = $errno = $httpCode = $totalTime = $result = $response = [];
        //close the handles
        foreach ($chStack as $i => $ch) {
            $error[$i] = curl_error($ch);
            $errno[$i] = curl_errno($ch);
            $httpCode[$i] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $totalTime[$i] = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
            $result[$i] = curl_multi_getcontent($ch); // get the content

            curl_multi_remove_handle($mh, $ch);
        }
        curl_multi_close($mh);

        foreach ($result as $i => $v) {
            $response[] = [
                'httpCode' => $httpCode[$i],
                'errno' => $errno[$i],
                'error' => $error[$i],
                'result' => $v,
                'totalTime' => $totalTime[$i],
            ];
        }

        return $response;
    }
}